package com.onetoall.yjt;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by DELL on 2017/1/4.
 */

public class LvOutAdapter extends BaseAdapter {

    private int pageSize = 1;
    private int pageCount = 10;
    private Context context;
    private List<String> datas;
    private boolean[] status;
    private  OnItemClickListener itemClickListener;
    private  ListView listView;
    private  int buju;
//    private int type;
    public LvOutAdapter(Context context, List<String> datas, boolean[] statis, ListView linearLayout, int buju){
        this.context = context;
        this.datas = datas;
        this.status = statis;
        this.listView = linearLayout;
        this.buju = buju;
//      this.type=type;
        Log.e("TAG",linearLayout.getHeight()+"||"+buju+"布局");
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


            MyHolder holder =null;
            if (convertView==null){
                convertView = LayoutInflater.from(context).inflate(R.layout.item_outer,null);
                holder = new MyHolder();
                holder.textView = (TextView) convertView.findViewById(R.id.tv_content);
                holder.rlOuter = (RelativeLayout) convertView.findViewById(R.id.rl_outer);
                holder.rvOuter = (RecyclerView) convertView.findViewById(R.id.rv_inners);
//            holder.sv = (ScrollView) convertView.findViewById(sv);
                convertView.setTag(holder);
            }else {
                holder = (MyHolder) convertView.getTag();
            }

            setDatas(position,holder);




        return convertView;
    }

    class MyHolder {
        TextView textView;
        RelativeLayout rlOuter;
        RecyclerView rvOuter;
//        ScrollView sv;
    }

    private void setDatas(final int position, final MyHolder holder){
        holder.textView.setText(datas.get(position));
        if (status[position]){
            holder.rvOuter.setVisibility(View.VISIBLE);
        }else {
            holder.rvOuter.setVisibility(View.GONE);
        }
        final List<String> datas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            datas.add("内层"+i);
        }
        Log.e("TAG","item high"+holder.rlOuter.getHeight());
//        ViewTreeObserver vto = holder.rlOuter.getViewTreeObserver();
//        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                holder.rlOuter.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//                holder.rlOuter.getHeight();
//                Log.e("TAG"," getViewTreeObserver item high"+holder.rlOuter.getHeight());
//                int setHigh =buju- holder.rlOuter.getHeight()*2;
//                ViewGroup.LayoutParams params = holder.rvOuter.getLayoutParams();
//                params.height =setHigh;
//                holder.rvOuter.setLayoutParams(params);
//            }
//        });
        int itemHigt = DesyUtil.dip2px(context,65);
        int setHigh =buju- itemHigt*2;
                ViewGroup.LayoutParams params = holder.rvOuter.getLayoutParams();
                params.height =setHigh;
                holder.rvOuter.setLayoutParams(params);
        final OutAdapter outAdapter = new OutAdapter(context,datas);
        final LinearLayoutManager manager = new LinearLayoutManager(context);

        holder.rvOuter.setLayoutManager(manager);
        holder.rvOuter.setAdapter(outAdapter);
        holder.rlOuter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i <status.length; i++) {
                    if (i!=position){
                        status[i] = false;
                    }
                }
                status[position] = !status[position];
                Log.e("TAG", Arrays.toString(status));
                if (status[position]){
                    holder.rvOuter.setVisibility(View.VISIBLE);
                }else {
                    holder.rvOuter.setVisibility(View.GONE);
                }
//                LvOutAdapter.this.notifyDataSetChanged();
                itemClickListener.click(position);
            }
        });
        holder.rvOuter.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
//                if (outAdapter.getFootStatus() == OutAdapter.TYPE_FOOTER_ADDMORE
//                        || !outAdapter.isShowFooter()) {
//                    return;
//                }
                Log.e("TAG","滚动了内层！！");
                Log.e("TAG",dy+"高度");
                if (!recyclerView.canScrollVertically(1)){
                    Log.e("TAG","滚动到了底部！！");

                    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            outAdapter.updateFootStatus(OutAdapter.TYPE_FOOTER_ADDMORE);
                            outAdapter.setShowFooter(true);
                            outAdapter.notifyDataSetChanged();
//                            try {
//                                Thread.sleep(1000);
//                                outAdapter.setShowFooter(false);
//                                outAdapter.notifyDataSetChanged();
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
                            datas.add("1111");
                            datas.add("2222");
                            outAdapter.setShowFooter(false);
                            outAdapter.notifyDataSetChanged();

                        }
                    });
                }
//                if (manager.findViewByPosition(manager.findFirstVisibleItemPosition()).getTop() == 0
//                        && manager.findFirstVisibleItemPosition() == 0) {
//
//                    outAdapter.updateFootStatus(OutAdapter.TYPE_FOOTER_ADDMORE);
//                    outAdapter.notifyDataSetChanged();
//                }

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            }
        });




    }

    public interface OnItemClickListener{
        void click(int position);
    }
    public void setOnitemClickListener(OnItemClickListener itemClickListener){
        if (itemClickListener!=null){
            this.itemClickListener = itemClickListener;
        }
    }

}
